package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.UserGoalPhase;

@Repository("userGoalPhaseDAO")
public class UserGoalPhaseDAOImpl extends
		GenericDaoHibernate<UserGoalPhase, Long> implements UserGoalPhaseDAO {

	public UserGoalPhaseDAOImpl() {
		super(UserGoalPhase.class);
	}
	
	public boolean exists(Long userId, Long goalId, String taskName) {
		DetachedCriteria dc = DetachedCriteria.forClass(UserGoalPhase.class);
		dc.add(Restrictions.eq("userGoal.pk.user.id", userId));
		dc.add(Restrictions.eq("userGoal.pk.goal.id", goalId));
		dc.add(Restrictions.eq("phaseName", taskName));
		getByStatus(dc, DataStatus.USING);
		
		List<UserGoalPhase> phases = findByCriteria(dc);
		
		return phases.size() > 0;
	}
	
	public List<UserGoalPhase> findByUserGoalId(Long userId, Long goalId){
		DetachedCriteria dc = DetachedCriteria.forClass(UserGoalPhase.class);
		dc.add(Restrictions.eq("userGoal.pk.user.id", userId));
		dc.add(Restrictions.eq("userGoal.pk.goal.id", goalId));
		getByStatus(dc, DataStatus.USING);
		
		List<UserGoalPhase> phases = findByCriteria(dc);
		
		return phases;		
	}
}
